#include "header.h"


int err_stop (char* msg)
{
	printf("%s \n",msg);
	exit(-1);
}


int open_audio_device_MuLaw(char *name, int mode){
	int status,arg, fd;

  	if ((fd = open (name, mode, 0)) == -1)
    {
      err_stop ("Unable to open audio device");
      
    }

	/*
	Setup the device. Note that it's important to set the sample format, 
	number of channels and sample rate exactly in this order. Some 
	devices depend on the order.
	*/
	
	/* Set the sample format */

  	arg = AFMT_MU_LAW;	   /* sample size */
  		status = ioctl(fd, SNDCTL_DSP_SETFMT, &arg);
  	
  	/*arg = SIZE;	   // sample size 
  	status = ioctl(fd, SOUND_PCM_WRITE_BITS, &arg);
  	if (status == -1)
    	printf("SOUND_PCM_WRITE_BITS ioctl failed");
  	if (arg != SIZE)
    	printf("unable to set sample size");
  	arg = CHANNELS;  // mono or stereo 
  	status = ioctl(fd, SOUND_PCM_WRITE_CHANNELS, &arg);
  	if (status == -1)
    	printf("SOUND_PCM_WRITE_CHANNELS ioctl failed");
  	if (arg != CHANNELS)
    	printf("unable to set number of channels");
  	arg = RATE;	   // sampling rate 
  	status = ioctl(fd, SOUND_PCM_WRITE_RATE, &arg);
  	if (status == -1)
    	printf("SOUND_PCM_WRITE_WRITE ioctl failed");
    //;
   /*
    No need for error checking because we will automatically adjust the
    signal based on the actual sample rate. However most application must 
    check the value of sample_rate and compare it to the requested rate.
    Small differences between the rates (10% or less) are normal and the 
    applications should usually tolerate them. However larger differences 
    may cause annoying pitch problems (Mickey Mouse).
    */

  	return fd;
}

int open_audio_device_PCM(char *name, int mode){
	
	int tmp,arg,status, fd;

  	if ((fd = open (name, mode, 0)) == -1)
    {
      err_stop ("Unable to open audio device");
    }

	/*
	Setup the device. Note that it's important to set the sample format, 
	number of channels and sample rate exactly in this order. Some 
	devices depend on the order.
	*/
	
	//arg = AFMT_MU_LAW;	   /* sample size */
  	//status = ioctl(fd, SNDCTL_DSP_SETFMT, &arg);
  	
  	//arg = AFMT_S8;
	   /* sample size */
  	//status = ioctl(fd, SNDCTL_DSP_SETFMT, &arg);
	/* Set the sample format */
  	/*arg = SIZE;	   // sample size 
  	status = ioctl(fd, SOUND_PCM_WRITE_BITS, &arg);
  	if (status == -1)
    	printf("SOUND_PCM_WRITE_BITS ioctl failed");
  	if (arg != SIZE)
    	printf("unable to set sample size");
  	arg = CHANNELS;  // mono or stereo 
  	status = ioctl(fd, SOUND_PCM_WRITE_CHANNELS, &arg);
  	if (status == -1)
    	printf("SOUND_PCM_WRITE_CHANNELS ioctl failed");
  	if (arg != CHANNELS)
    	printf("unable to set number of channels");
  	arg = RATE;	   // sampling rate 
  	status = ioctl(fd, SOUND_PCM_WRITE_RATE, &arg);
  	if (status == -1)
    	printf("SOUND_PCM_WRITE_WRITE ioctl failed");*/
    
   /*
    No need for error checking because we will automatically adjust the
    signal based on the actual sample rate. However most application must 
    check the value of sample_rate and compare it to the requested rate.
    Small differences between the rates (10% or less) are normal and the 
    applications should usually tolerate them. However larger differences 
    may cause annoying pitch problems (Mickey Mouse).
    */

  	return fd;
}